package maosui.websocket;

/**
 * Created by wdj on 14-8-7.
 */

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class SDB {

    private static Configuration configuration = null;
    private static SessionFactory sessionFactory = null;
    private static ServiceRegistry serviceRegistry = null;

    static {
        try {
            configuration = new Configuration().configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            System.out.println("SDB init");
        } catch (HibernateException e) {
            //e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public static org.hibernate.Session getSession() throws HibernateException {
        if(sessionFactory == null){
            return null;
        }
        return sessionFactory.openSession();
    }
}

